package net.amsoft.iservice.isso.hibernate.tests;
import java.util.ArrayList;

import net.amsoft.iservice.isso.hibernate.IBroker;
import net.amsoft.iservice.isso.hibernate.Principal;
import net.amsoft.iservice.isso.hibernate.PrincipalMetaData;
import net.amsoft.iservice.isso.hibernate.Synonym;
import net.amsoft.iservice.isso.util.ISSOConstants;
import net.amsoft.iservice.isso.util.ISSOHelpers;
import net.amsoft.iservice.util.HibernateUtil;
import net.amsoft.iservice.util.exception.IServiceException;

public class PrincipalTest
{
    private static int loopBroker = 0;

    public static void makePrincipal() throws IServiceException {
        int bId = 1;
        Principal oPrincipal = new Principal();
        if (bId > 0) {
            IBroker oIB = (IBroker) ISSOHelpers.fetch(IBroker.class, new Long(
                    bId));
            oPrincipal.setBroker(oIB);
        }
        oPrincipal.setState((short) ISSOConstants.STATE_PENDING);

        oPrincipal.insert();

        loopBroker++;
    }

    protected static void makeSynonym(int sId, int gIN, String name) {

        Synonym oS = new Synonym();
        oS.setSynonym(name);
        oS.setState((short) ISSOConstants.STATE_ACTIVE);
        if (sId > 0) {
            Principal oIB = (Principal) ISSOHelpers.fetch(Principal.class,
                    new Long(sId));
            oS.setPrincipal(oIB);

        }
        if (gIN > 0) {
            Synonym oGINSyn = (Synonym) ISSOHelpers.fetchSynonym(Synonym.class,
                    new Long(gIN));
            oS.setGlobalINumber(oGINSyn);
        }

        try {
            oS.insert();
        } catch (IServiceException e) {

            e.printStackTrace();
        }

    }

    protected static void makePrincipalMetaData(int sId) {
        PrincipalMetaData oPMD = new PrincipalMetaData();
        oPMD.setChallengeQuestionId(1);
        if (sId > 0) {
            Principal oIB = (Principal) ISSOHelpers.fetch(Principal.class,
                    new Long(sId));
            oPMD.setPrincipal(oIB);
        }
        oPMD.setEncActivationToken("ENCACTTOKEN");
        oPMD.insert();

    }

    private static void updatePrincipalData() throws IServiceException {
        PrincipalMetaData oData = (PrincipalMetaData) ISSOHelpers
                .fetchMetaData(new Long(1));
        oData.setChallengeQuestionId(5);
        oData.setEmail("something@something.com");
        oData.update();
    }

    public static void main(String[] args) throws IServiceException {

        IBroker oIB = (IBroker) ISSOHelpers
                .fetchIBroker("isso.ibroker0.amsoft.co.in");

        IBrokerTest iBT = new IBrokerTest();
        IBrokerTest.mB();
        System.out.println("--------------CREATE------------------");
        loopBroker = 0;
        makePrincipal();
        //      create synonym
        makeSynonym(1, 0, "!!1001!1001");
        makeSynonym(1, 1, "=foo");

        //        makePrincipalMetaData(1);
        //        //Create Machine Cookie
        //        CookieTest.makeMachineCookie(1);
        //        //Create SessionCookie
        //        CookieTest.makeSessionCookie(1);

        //Update Principal
        //        updatePrincipalData();
        //
        //        Principal oP = (Principal) ISSOHelpers.fetch(Principal.class, new
        // Long(
        //                1));
        //        System.out.println(">>>>>>>>>>>>>>>" + oP.getCreationTime() +">>>>" +
        // oP.getPrincipalMetaData().size()
        //                 );
        //        
        //        Iterator it = oP.getPrincipalMetaData().iterator();
        //        while (it.hasNext()) {
        //            // Get element
        //            Object element = it.next();
        //            System.out.println(element.toString());
        //        }

        //       makePrincipal();
        //// // create synonym
        //        makeSynonym(2, 0, "!!1001!1001");
        //        makeSynonym(2, 3, "=bar");
        //
        //        makePrincipalMetaData(2);
        //        //Create Machine Cookie
        //        CookieTest.makeMachineCookie(2);
        //        //Create SessionCookie
        CookieTest.makeSessionCookie(2);
        //        ArrayList ar = new ArrayList();
        //        ar.add("=foo");
        //        ArrayList ar1 = new ArrayList();
        //        ar1.add("=foo");
        //        subscribe("!11222" , 1 , ar);
        //        subscribe("!11222233" , 1 , ar1);
        HibernateUtil.getSessionFactory().close();
    }

    public static void subscribe(String globalInumber, int serverName,
            ArrayList synonymList) throws IServiceException {
        IBroker oIBroker = (IBroker) ISSOHelpers.fetch(IBroker.class, new Long(
                serverName));

        Principal oSub = new Principal();
        oSub.setBroker(oIBroker);
        oSub = (Principal) oSub.insert();

        Synonym oGINSyn = new Synonym();
        oGINSyn.setSynonym(globalInumber);
        oSub.addSynonym(oGINSyn);

        for (int i = 0; i < synonymList.size(); i++) {
            Synonym oSyn = new Synonym();
            oSyn.setGlobalINumber(oGINSyn);
            oSyn.setSynonym((String) synonymList.get(i));

            oSub.addSynonym(oSyn);
        }
        oSub = (Principal) oSub.update();
    }
}